package com.bbkb.user.service.impl;

import com.bbkb.common.page.PageResult;
import com.bbkb.common.util.PageResultUtil;
import com.bbkb.user.dao.SysRoleRelMapper;
import com.bbkb.user.dao.SysUserMapper;
import com.bbkb.user.domain.SysUser;
import com.bbkb.user.dto.SysUserDTO;
import com.bbkb.user.service.ISysUserService;
import com.bbkb.user.util.UserConvert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/2/3 0003.
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleRelMapper sysRoleRelMapper;

    @Override
    public SysUserDTO getByUserName(String userName) {
        SysUser user = sysUserMapper.findOneByUserName(userName);
        return UserConvert.sysUser2Dto(user);
    }

    @Override
    public PageResult<SysUserDTO> querySysUserPage(SysUserDTO sysUser) {
        PageHelper.startPage(sysUser.getPage(), sysUser.getRows());
        List<SysUser> list = sysUserMapper.selectByAll(sysUser);
        return PageResultUtil.convert(new PageInfo<>(list), UserConvert::sysUser2Dto);
    }

    @Override
    public void updateStatus(Long id, String status) {
//        SysUser sysUser = new SysUser();
//        sysUser.setId(id);
//        sysUser.setStatus(status);
//        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return;
    }

    @Override
    public SysUserDTO getSysUserById(Long id) {
//        return sysUserMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    @Transactional
    public void addSysUser(SysUserDTO sysUser) {
//        sysUserMapper.insertSelective(sysUser);
//        addSysRoleRel(sysUser);
        return;
    }

    @Override
    @Transactional
    public void updateSysUser(SysUserDTO sysUser) {
//        sysRoleRelMapper.deleteByObjId(sysUser.getId(), RoleTypeEnum.USER.getKey());
//        addSysRoleRel(sysUser);
//        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return;
    }

    public void addSysRoleRel(SysUserDTO sysUser) {
//        SysRoleRel sysRoleRel = new SysRoleRel();
//        sysRoleRel.setRoleId(sysUser.getRoleId());
//        sysRoleRel.setRelType(RoleTypeEnum.USER.getKey());
//        sysRoleRel.setObjId(sysUser.getId());
//        sysRoleRelMapper.insertSelective(sysRoleRel);
        return;
    }

    @Override
    public List<SysUserDTO> getSysUsersByRoleId(Long roleId) {
//        return sysUserMapper.getSysUsersByRoleId(roleId);
        return null;
    }
}
